<?php
// 收货地址
namespace app\shop\controller;

use think\Db;
use think\Session;
use think\Request;
use think\Controller;
use app\shop\model\Area as AreaModel;
use app\shop\model\GoodsType as GoodsTypeModel;
use app\shop\model\MemberAddress as MemberAddressModel;

class Address extends Controller
{
    public function index(MemberAddressModel $memberAddress, GoodsTypeModel $goodsType)
    {
        // 会员信息
        $member_id = Session::get('member_id');

        // 收货地址
        $data = $memberAddress->where('member_id', $member_id)->order('status', 'desc')->select();


        return $this->fetch('', [
            'mate_head' => '收货地址',
            'data'      => $data

        ]);
    }


    /**
     * 添加收货地址（结算页面）
     */
    public function account_address()
    {
        // 获取数据
        $data = Request::instance()->param();

        // 所在地区(一级)
        $area = (new AreaModel())->where('area_parent_id', 0)->order('area_sort desc')->field('area_id,area_name')->select();

        return $this->fetch('', [
            'area' => $area,
        ]);

    }


    /**
     * 修改地址
     */
    public function edit_address()
    {
        // 获取数据
        $id = Request::instance()->param('id');

        $data = (new MemberAddressModel())->where(['id' => $id])->find();

        $area_info2 = (new AreaModel)->where(['area_id' => $data['area_id']])->find()->toArray();

        $area_info1 = (new AreaModel)->where(['area_id' => $area_info2['area_parent_id']])->find()->toArray();


        // 所在地区(一级)
        $area = (new AreaModel())->where('area_parent_id', 0)->order('area_sort desc')->field('area_id,area_name')->select();

        return $this->fetch('', [
            'area'       => $area,
            'data'       => $data,
            'area_info'  => $area_info2,
            'area_info1' => $area_info1,

        ]);
    }


    /**
     *编辑地址
     */
    public function edit_address0()
    {
        if (Request::instance()->isPost()) {
            // 获取信息
            $data = Request::instance()->param();

            Db::startTrans();
            try {
                if ($data['status'] == 1) {
                    Db::name('member_address')->where(['member_id' => Session::get('member_id'), 'status' => 1])->update(['status' => 0]);
                }

                $data['area_info'] = $data['area_info1'] . $data['area_info2'] . $data['area_info3'];
                $operation = (new MemberAddressModel())->allowField(true)->save($data, ['id' => $data['id']]);

                Db::commit();

                if ($operation || $operation == 0) {
                    return ['code' => 200];
                } else {
                    return ['code' => 402];
                }

            } catch (\Exception $e) {

                // 回滚事务
                Db::rollback();

                return ['code' => 402];
            }


        }
    }


    /**
     * 结算页面选择更多地址
     */
    public function other_address()
    {
        // 获取用户ID
        $address_id = Request::instance()->param('address_id');
        // 获取用户地址
        $find = (new MemberAddressModel())->where('id', $address_id)->find();

        return ['code' => 200, 'id' => $find['id'], 'name' => $find['name'], 'phone' => $find['phone'], 'area_info' => $find['area_info'], 'address' => $find['address']];
    }
}